package cc.rengu.igas.channel.upac.core.service.trans;

import cc.rengu.igas.channel.upac.common.constant.UpacConfigConstant;
import cc.rengu.igas.channel.upac.common.constant.UpacParamConstant;
import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.common.util.UpacSecurityUtil;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.CollectionPayService;
import cc.rengu.igas.channel.upac.facade.bean.CustomerInfo;
import cc.rengu.igas.channel.upac.facade.bean.RiskCtrlInfo;
import cc.rengu.igas.channel.upac.facade.bean.TokenPayInfo;
import cc.rengu.igas.channel.upac.facade.request.CollectionSignWithSmsRequest;
import cc.rengu.igas.channel.upac.facade.response.CollectionSignWithSmsResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.CertInfoMapper;
import cc.rengu.oltp.service.common.dao.SecPlanInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.CertInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.SecPlanInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.CertInfo;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SecPlanInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RSAUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.codec.binary.Base64;

import java.security.interfaces.RSAPublicKey;
import java.util.List;
import java.util.Optional;


/**
 * 发送短信验证码 on 2019/10/8.
 */
public class CollectionSignWithSmsService extends CollectionPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        CollectionSignWithSmsRequest signWithSmsRequest = new CollectionSignWithSmsRequest();
        ConvertUtil.convertOutput(signWithSmsRequest);
        return signWithSmsRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        CollectionSignWithSmsResponse signWithSmsResponse = new CollectionSignWithSmsResponse();
        CollectionSignWithSmsRequest signWithSmsRequest = (CollectionSignWithSmsRequest) request;
        String payAcctNo = signWithSmsRequest.getPayAcctNo();
        if (payAcctNo == null) {
            rglog.error("未上送账户");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        if (AppParamConstant.YES.equals(acctEncFlag)) {
            signWithSmsRequest.setPayAcctNo(encryptData(payAcctNo));
        }
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(signWithSmsRequest);
        /*customer信息转换*/
        if (null != signWithSmsRequest.getCustomerInfo()) {
            packageCustomerInfo(signWithSmsRequest.getCustomerInfo(), signWithSmsRequest.getPayAcctNo());
        }
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "77");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "12");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.COLLECTION.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, signWithSmsRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            signWithSmsResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            signWithSmsResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(signWithSmsResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(signWithSmsResponse);
        signWithSmsResponse.setRespCode(convertRespCode(signWithSmsResponse.getRespCode()));

        chnlBizResponse.setResult(signWithSmsResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        CollectionSignWithSmsResponse collectionSignWithSmsResponse = (CollectionSignWithSmsResponse) response.getResult();
        ConvertUtil.convertInput(collectionSignWithSmsResponse);
    }

    /**
     * 账号加密处理
     *
     * @param cardNo 支付账号
     * @throws Exception 异常
     */
    private String encAcctNo(String cardNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String encoding = XmlConfigUtil.getXmlConfigCacheKeyValue(UpacConfigConstant.UPAC_CONF_NODE, UpacConfigConstant.UPAC_ENCODING);
        encoding = (null == encoding) ? UpacParamConstant.UTF_8_ENCODING : encoding;
        String acctNoEncType = XmlConfigUtil.getXmlConfigCacheKeyValue(UpacConfigConstant.UPAC_CONF_NODE, UpacConfigConstant.UPAC_ACCT_ENC);
        if ("0".equals(acctNoEncType)) {
            return cardNo;
        } else {
            /* 获取敏感信息加密证书 */
            String secPlanInfoListString = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.SEC_PLAN_INFO_LIST);
            List<SecPlanInfo> secPlanInfoList = JSONArray.parseArray(secPlanInfoListString, SecPlanInfo.class);
            Optional<SecPlanInfo> encryptSecPlanOpt = secPlanInfoList.stream().filter(item -> item.getSecPlanType().equals(UpacParamConstant.ENCRYPT_CERT_TYPE)).findFirst();
            if (encryptSecPlanOpt.isPresent()) {
                CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
                CertInfo encryptCertInfo = certInfoMapper.selectCertInfoByPrimaryKey(encryptSecPlanOpt.get().getSecPlanId() + encryptSecPlanOpt.get().getSecPlanType());
                xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.encryptCertId, encryptCertInfo.getCertId());
                return UpacSecurityUtil.encryptData(cardNo, encryptCertInfo.getCertValue(), encoding);
            }
        }
        return cardNo;
    }

    /**
     * 个人身份及认证信息加密处理
     *
     * @param customerInfo 个人身份及认证信息
     * @param cardNo       支付账号
     * @throws Exception 异常
     */
    protected void packageCustomerInfo(CustomerInfo customerInfo, String cardNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        CertInfo encryptCertInfo = null;
        String encoding = UpacParamConstant.UTF_8_ENCODING;
        String pubKey = null;
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        if (instId == null) {
            instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.HEADER_INST_ID);
        }
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        DstChannelCfg dstChannelCfg = (DstChannelCfg) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_CFG, DstChannelCfg.class);
        StringBuffer customerEncryptInfo = new StringBuffer(UpacParamConstant.LEFT_BRACE);
        if (null != customerInfo.getCertType() && !customerInfo.getCertType().isEmpty()) {
            customerEncryptInfo.append(UpacTreeNodeConstant.certType + UpacParamConstant.EQUAL + customerInfo.getCertType() + UpacParamConstant.AMPERSAND);
        }
        if (null != customerInfo.getCertNo() && !customerInfo.getCertNo().isEmpty()) {
            customerEncryptInfo.append(UpacTreeNodeConstant.certNo + UpacParamConstant.EQUAL + customerInfo.getCertNo() + UpacParamConstant.AMPERSAND);
        }
        if (null != customerInfo.getCustName() && !customerInfo.getCustName().isEmpty()) {
            customerEncryptInfo.append(UpacTreeNodeConstant.custName + UpacParamConstant.EQUAL + customerInfo.getCustName() + UpacParamConstant.AMPERSAND);
        }
        if (null != customerInfo.getAuthCode() && !customerInfo.getAuthCode().isEmpty()) {
            customerEncryptInfo.append(UpacTreeNodeConstant.smsCode + UpacParamConstant.EQUAL + customerInfo.getAuthCode() + UpacParamConstant.AMPERSAND);
        }
        if (null != customerInfo.getCvn2() || null != customerInfo.getPhoneNo() || null != customerInfo.getExpiryDate() || null != customerInfo.getPinData()) {
            /* 获取敏感信息加密证书 */
            List<SecPlanInfo> secPlanInfoList = null;
            String secPlanInfoListString = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.SEC_PLAN_INFO_LIST);
            if (secPlanInfoListString == null) {
                /* 根据通道安全计划获取安全计划 */
                SecPlanInfoMapper secPlanInfoMapper = new SecPlanInfoMapperImpl();
                secPlanInfoList = secPlanInfoMapper.selectSecPlanInfoBySecPlanId(instId, dstChannelCfg.getSecPlanId());
                if (null != secPlanInfoList && !secPlanInfoList.isEmpty()) {
                    xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.SEC_PLAN_INFO_LIST, JSON.toJSONString(secPlanInfoList));
                } else {
                    rglog.error("获取通道解密密钥证书失败！");
                    throw new BizException(RespCodeEnum.GET_KEY_INFO_ERROR.getRespCode(), RespCodeEnum.GET_KEY_INFO_ERROR.getRespDesc());
                }
            } else {
                secPlanInfoList = JSONArray.parseArray(secPlanInfoListString, SecPlanInfo.class);
            }
            Optional<SecPlanInfo> encryptSecPlanOpt = secPlanInfoList.stream().filter(item -> item.getSecPlanType().equals(UpacParamConstant.ENCRYPT_CERT_TYPE)).findFirst();
            if (encryptSecPlanOpt.isPresent()) {
                CertInfoMapper certInfoMapper = new CertInfoMapperImpl();
                encryptCertInfo = certInfoMapper.selectCertInfoByPrimaryKey(instId + encryptSecPlanOpt.get().getSecPlanId() + encryptSecPlanOpt.get().getSecPlanType());
                if (encryptCertInfo == null) {
                    rglog.error("获取通道解密密钥证书失败！");
                    throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
                } else {
                    xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.encryptCertId, encryptCertInfo.getCertId());
                }
            }
            if ("0".equals(encryptCertInfo.getCertSaveType())) {
                pubKey = encryptCertInfo.getCertValue();
            } else {
                /*优先从系统缓存读取*/
                RSAPublicKey publicKey = null;
                pubKey = (String) UnifiedCache.get(UpacParamConstant.CERT_KEY_CACHE, encryptCertInfo.getCertIndex());
                if (pubKey == null) {
                    /*从本地证书文件获取*/
                    publicKey = RSAUtil.getPublicKeyFromCertFile(encryptCertInfo.getCertSavePath());
                    if (publicKey != null) {
                        pubKey = Base64.encodeBase64String(publicKey.getEncoded());
                        if (UnifiedCache.set(UpacParamConstant.CERT_KEY_CACHE, encryptCertInfo.getCertIndex(), Base64.encodeBase64String(publicKey.getEncoded())) != 0) {
                            rglog.error("设置证书缓存索引号{}缓存失败", encryptCertInfo.getCertIndex());
                        }
                    }
                }
            }
        }
        /* 客户密钥加密 */
        if (null != customerInfo.getPinData() && !customerInfo.getPinData().isEmpty()) {
            if (null == cardNo || cardNo.trim().isEmpty()) {
                rglog.error("客户送了密码（PIN），必须上传卡号！");
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            String encryptPinData = UpacSecurityUtil.encryptPinData(cardNo, customerInfo.getPinData(), pubKey, encoding);
            customerEncryptInfo.append(UpacTreeNodeConstant.pinData + UpacParamConstant.EQUAL + encryptPinData + UpacParamConstant.AMPERSAND);
        }
        /* 敏感信息加密 */
        StringBuffer encryptedInfo = new StringBuffer(UpacParamConstant.BLANK);
        if (null != customerInfo.getPhoneNo() && !customerInfo.getPhoneNo().isEmpty()) {
            encryptedInfo.append(UpacTreeNodeConstant.phoneNo).append(UpacParamConstant.EQUAL).append(customerInfo.getPhoneNo()).append(UpacParamConstant.AMPERSAND);
        }
        if (null != customerInfo.getCvn2() && !customerInfo.getCvn2().isEmpty()) {
            encryptedInfo.append(UpacTreeNodeConstant.cvn2).append(UpacParamConstant.EQUAL).append(customerInfo.getCvn2()).append(UpacParamConstant.AMPERSAND);
        }
        if (null != customerInfo.getExpiryDate() && !customerInfo.getExpiryDate().isEmpty()) {
            encryptedInfo.append(UpacTreeNodeConstant.expired).append(UpacParamConstant.EQUAL).append(customerInfo.getExpiryDate()).append(UpacParamConstant.AMPERSAND);
        }
        if (!"".equals(encryptedInfo.toString())) {
            xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.encryptCertId, encryptCertInfo.getCertId());
            encryptedInfo.setLength(encryptedInfo.length() - 1);
            String encryptData = UpacSecurityUtil.encryptData(encryptedInfo.toString(), pubKey, encoding);
            customerEncryptInfo.append(UpacTreeNodeConstant.encryptedInfo + UpacParamConstant.EQUAL + encryptData);
            //customerEncryptInfo.append(UpacParamConstant.encryptedInfo + UpacParamConstant.EQUAL + encryptData + UpacParamConstant.AMPERSAND);
        } else {
            customerEncryptInfo.setLength(customerEncryptInfo.length() - 1);
        }
        customerEncryptInfo.append(UpacParamConstant.RIGHT_BRACE);
        String customerInfoString = new String(Base64.encodeBase64(customerEncryptInfo.toString().getBytes(encoding)), encoding);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.customerInfo, customerInfoString);
    }

    /**
     * 风控信息KV转换处理
     *
     * @param riskCtrlInfo 风控信息
     * @throws Exception 异常
     */
    protected void packageRiskCtrlInfo(RiskCtrlInfo riskCtrlInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        StringBuffer riskCtrlData = new StringBuffer(UpacParamConstant.LEFT_BRACE);
        if (null != riskCtrlInfo.getShippingFlag() && !riskCtrlInfo.getShippingFlag().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingFlag + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingFlag() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getShippingCountry() && !riskCtrlInfo.getShippingCountry().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingCountryCode + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingCountry() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getShippingProvince() && !riskCtrlInfo.getShippingProvince().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingProvinceCode + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingProvince() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getShippingCity() && !riskCtrlInfo.getShippingCity().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingCityCode + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingCity() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getShippingArea() && !riskCtrlInfo.getShippingArea().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingDistrictCode + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingArea() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getShippingStreet() && !riskCtrlInfo.getShippingStreet().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingStreet + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingStreet() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getGoodCategory() && !riskCtrlInfo.getGoodCategory().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.commodityCategory + UpacParamConstant.EQUAL + riskCtrlInfo.getGoodCategory() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getGoodName() && !riskCtrlInfo.getGoodName().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.commodityName + UpacParamConstant.EQUAL + riskCtrlInfo.getGoodName() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getGoodUrl() && !riskCtrlInfo.getGoodUrl().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.commodityUrl + UpacParamConstant.EQUAL + riskCtrlInfo.getGoodUrl() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getGoodPrice() && !riskCtrlInfo.getGoodPrice().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.commodityUnitPrice + UpacParamConstant.EQUAL + riskCtrlInfo.getGoodPrice() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getGoodNum() && !riskCtrlInfo.getGoodNum().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.commodityQty + UpacParamConstant.EQUAL + riskCtrlInfo.getGoodNum() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getShippingMobile() && !riskCtrlInfo.getShippingMobile().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.shippingMobile + UpacParamConstant.EQUAL + riskCtrlInfo.getShippingMobile() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getAddrModifyTime() && !riskCtrlInfo.getAddrModifyTime().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.addressModifyTime + UpacParamConstant.EQUAL + riskCtrlInfo.getAddrModifyTime() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getUserRegisterTime() && !riskCtrlInfo.getUserRegisterTime().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.userRegisterTime + UpacParamConstant.EQUAL + riskCtrlInfo.getUserRegisterTime() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getReceiptNameModifyTime() && !riskCtrlInfo.getReceiptNameModifyTime().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.orderNameModifyTime + UpacParamConstant.EQUAL + riskCtrlInfo.getReceiptNameModifyTime() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getUserId() && !riskCtrlInfo.getUserId().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.userId + UpacParamConstant.EQUAL + riskCtrlInfo.getUserId() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getReceiptName() && !riskCtrlInfo.getReceiptName().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.orderName + UpacParamConstant.EQUAL + riskCtrlInfo.getReceiptName() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getUserFlag() && !riskCtrlInfo.getUserFlag().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.userFlag + UpacParamConstant.EQUAL + riskCtrlInfo.getUserFlag() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getMobileModifyTime() && !riskCtrlInfo.getMobileModifyTime().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.mobileModifyTime + UpacParamConstant.EQUAL + riskCtrlInfo.getMobileModifyTime() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getRiskLevel() && !riskCtrlInfo.getRiskLevel().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.riskLevel + UpacParamConstant.EQUAL + riskCtrlInfo.getRiskLevel() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getMchntUserId() && !riskCtrlInfo.getMchntUserId().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.merUserId + UpacParamConstant.EQUAL + riskCtrlInfo.getMchntUserId() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getMchntUserRegTime() && !riskCtrlInfo.getMchntUserRegTime().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.merUserRegDt + UpacParamConstant.EQUAL + riskCtrlInfo.getMchntUserRegTime() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getMchntUserRegEmail() && !riskCtrlInfo.getMchntUserRegEmail().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.merUserEmail + UpacParamConstant.EQUAL + riskCtrlInfo.getMchntUserRegEmail() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getDiskSeqNo() && !riskCtrlInfo.getDiskSeqNo().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.diskSep + UpacParamConstant.EQUAL + riskCtrlInfo.getDiskSeqNo() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getImei() && !riskCtrlInfo.getImei().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.imei + UpacParamConstant.EQUAL + riskCtrlInfo.getImei() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getImei() && !riskCtrlInfo.getImei().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.imei + UpacParamConstant.EQUAL + riskCtrlInfo.getImei() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getMacAddr() && !riskCtrlInfo.getMacAddr().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.macAddr + UpacParamConstant.EQUAL + riskCtrlInfo.getMacAddr() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getLongitudeAndLatitude() && !riskCtrlInfo.getLongitudeAndLatitude().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.lbs + UpacParamConstant.EQUAL + riskCtrlInfo.getLongitudeAndLatitude() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getDeviceNo() && !riskCtrlInfo.getDeviceNo().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.deviceNumber + UpacParamConstant.EQUAL + riskCtrlInfo.getDeviceNo() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getDeviceType() && !riskCtrlInfo.getDeviceType().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.deviceType + UpacParamConstant.EQUAL + riskCtrlInfo.getDeviceType() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getCaptureMethod() && !riskCtrlInfo.getCaptureMethod().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.captureMethod + UpacParamConstant.EQUAL + riskCtrlInfo.getCaptureMethod() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getSimCardNum() && !riskCtrlInfo.getSimCardNum().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.simCardCount + UpacParamConstant.EQUAL + riskCtrlInfo.getSimCardNum() + UpacParamConstant.AMPERSAND);
        }
        if (null != riskCtrlInfo.getDeviceName() && !riskCtrlInfo.getDeviceName().isEmpty()) {
            riskCtrlData.append(UpacTreeNodeConstant.deviceName + UpacParamConstant.EQUAL + riskCtrlInfo.getDeviceName() + UpacParamConstant.AMPERSAND);
        }
        riskCtrlData.setLength(riskCtrlData.length() - 1);
        riskCtrlData.append(UpacParamConstant.RIGHT_BRACE);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.riskRateInfo, riskCtrlData.toString());
    }

    /**
     * 支付标识化信息KV转换处理
     *
     * @param tokenPayInfo 支付标记化信息
     */
    protected void packageTokenPayInfo(TokenPayInfo tokenPayInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        StringBuffer tokenPayData = new StringBuffer(UpacParamConstant.LEFT_BRACE);
        if (null != tokenPayInfo.getToken() && !tokenPayInfo.getToken().isEmpty()) {
            tokenPayData.append(UpacTreeNodeConstant.token + UpacParamConstant.EQUAL + tokenPayInfo.getToken() + UpacParamConstant.AMPERSAND);
        }
        if (null != tokenPayInfo.getTrId() && !tokenPayInfo.getTrId().isEmpty()) {
            tokenPayData.append(UpacTreeNodeConstant.trId + UpacParamConstant.EQUAL + tokenPayInfo.getTrId() + UpacParamConstant.AMPERSAND);
        }
        if (null != tokenPayInfo.getTokenLevel() && !tokenPayInfo.getTokenLevel().isEmpty()) {
            tokenPayData.append(UpacTreeNodeConstant.tokenLevel + UpacParamConstant.EQUAL + tokenPayInfo.getTokenLevel() + UpacParamConstant.AMPERSAND);
        }
        if (null != tokenPayInfo.getTokenBegin() && !tokenPayInfo.getTokenBegin().isEmpty()) {
            tokenPayData.append(UpacTreeNodeConstant.tokenBegin + UpacParamConstant.EQUAL + tokenPayInfo.getTokenBegin() + UpacParamConstant.AMPERSAND);
        }
        if (null != tokenPayInfo.getTokenEnd() && !tokenPayInfo.getTokenEnd().isEmpty()) {
            tokenPayData.append(UpacTreeNodeConstant.tokenEnd + UpacParamConstant.EQUAL + tokenPayInfo.getTokenEnd() + UpacParamConstant.AMPERSAND);
        }
        if (null != tokenPayInfo.getTokenType() && !tokenPayInfo.getTokenType().isEmpty()) {
            tokenPayData.append(UpacTreeNodeConstant.tokenType + UpacParamConstant.EQUAL + tokenPayInfo.getTokenType() + UpacParamConstant.AMPERSAND);
        }
        tokenPayData.setLength(tokenPayData.length() - 1);
        tokenPayData.append(UpacParamConstant.RIGHT_BRACE);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.tokenPayData, tokenPayData.toString());
    }
}
